{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I的词义Synset('iodine.n.01')的同义词为：iodine\n",
      "I的词义Synset('iodine.n.01')的同义词为：iodin\n",
      "I的词义Synset('iodine.n.01')的同义词为：I\n",
      "I的词义Synset('iodine.n.01')的同义词为：atomic_number_53\n",
      "I的词义Synset('one.n.01')的同义词为：one\n",
      "I的词义Synset('one.n.01')的同义词为：1\n",
      "I的词义Synset('one.n.01')的同义词为：I\n",
      "I的词义Synset('one.n.01')的同义词为：ace\n",
      "I的词义Synset('one.n.01')的同义词为：single\n",
      "I的词义Synset('one.n.01')的同义词为：unity\n",
      "I的词义Synset('i.n.03')的同义词为：I\n",
      "I的词义Synset('i.n.03')的同义词为：i\n",
      "I的词义Synset('one.s.01')的同义词为：one\n",
      "I的词义Synset('one.s.01')的同义词为：1\n",
      "I的词义Synset('one.s.01')的同义词为：i\n",
      "I的词义Synset('one.s.01')的同义词为：ane\n",
      "like的词义Synset('like.n.01')的同义词为：like\n",
      "like的词义Synset('like.n.01')的同义词为：the_like\n",
      "like的词义Synset('like.n.01')的同义词为：the_likes_of\n",
      "like的词义Synset('like.n.02')的同义词为：like\n",
      "like的词义Synset('like.n.02')的同义词为：ilk\n",
      "like的词义Synset('wish.v.02')的同义词为：wish\n",
      "like的词义Synset('wish.v.02')的同义词为：care\n",
      "like的词义Synset('wish.v.02')的同义词为：like\n",
      "like的词义Synset('like.v.02')的同义词为：like\n",
      "like的词义Synset('like.v.03')的同义词为：like\n",
      "like的词义Synset('like.v.04')的同义词为：like\n",
      "like的词义Synset('like.v.05')的同义词为：like\n",
      "like的词义Synset('like.a.01')的同义词为：like\n",
      "like的词义Synset('like.a.01')的同义词为：similar\n",
      "like的词义Synset('like.a.02')的同义词为：like\n",
      "like的词义Synset('like.a.02')的同义词为：same\n",
      "like的词义Synset('alike.a.01')的同义词为：alike\n",
      "like的词义Synset('alike.a.01')的同义词为：similar\n",
      "like的词义Synset('alike.a.01')的同义词为：like\n",
      "like的词义Synset('comparable.s.02')的同义词为：comparable\n",
      "like的词义Synset('comparable.s.02')的同义词为：corresponding\n",
      "like的词义Synset('comparable.s.02')的同义词为：like\n",
      "sandwiches的词义Synset('sandwich.n.01')的同义词为：sandwich\n",
      "sandwiches的词义Synset('sandwich.v.01')的同义词为：sandwich\n",
      "sandwiches的词义Synset('sandwich.v.02')的同义词为：sandwich\n",
      "扩展后的查询: i wish care alike same like I sandwich similar ilk atomic_number_53 ace corresponding 1 comparable iodine the_like the_likes_of unity ane iodin one single\n"
     ]
    }
   ],
   "source": [
    "# wordnet是一个英语词汇数据库，包含单词的同义词集（synsets）。\n",
    "from nltk.corpus import wordnet\n",
    "\n",
    "def expand_query(query):\n",
    "    synonyms = []\n",
    "\n",
    "    # 使用split()方法将查询字符串分割成单词列表，并遍历每个单词。\n",
    "    for word in query.split():\n",
    "\n",
    "        # 对于查询中的每个单词，使用wordnet.synsets(word)查找其所有同义词集（synsets）。每个synset代表一个语义概念\n",
    "        for syn in wordnet.synsets(word):\n",
    "\n",
    "            # 对于每个同义词集，遍历其包含的所有词元（lemmas）。每个lemma是一个具体的单词形式。\n",
    "            for lemma in syn.lemmas():\n",
    "                print(f\"{word}的词义{syn}的同义词为：{lemma.name()}\")\n",
    "                synonyms.append(lemma.name())\n",
    "\n",
    "    # 使用set（）来去重并使用.join拼接同义词\n",
    "    expanded_query = \" \".join(set(synonyms))\n",
    "    \n",
    "    return expanded_query\n",
    "\n",
    "query = \"I like sandwiches\"\n",
    "expanded_query = expand_query(query)\n",
    "print(f\"扩展后的查询: {expanded_query}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[nltk_data] Downloading package wordnet to\n",
      "[nltk_data]     C:\\Users\\admin\\AppData\\Roaming\\nltk_data...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import nltk\n",
    "nltk.download('wordnet')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
